var bandera = 0;
var nextSpriteY = 507;
var nextSpriteX = 23;
var vx = 0;
var vy = 1;
var gravity = 3;
var limite = 150;
var bajando = false;
var jumping = false;
var velY = -20;
var timer = 0.5;
var tocaTierra = false;

function jump () {

	if (jsGFwk._gameObjects.mario.y > limite && bajando){
	jsGFwk._gameObjects.mario.y -= vy * velUp;				
	}else{
	jsGFwk._gameObjects.mario.y += vy + gravity;				
	
	}
}
	

var mario = {
	id: "mario",
	init: function () {	},
	visible: true,
	
	y: 0,
	x: 100,
	
	
	update: function(delta) {
			
			var resultado = jsGFwk._gameObjects.mapa._getGridRelation(this.x, this.y) //colision izquierda superior
			var resultado1 = jsGFwk._gameObjects.mapa._getGridRelation(this.x, this.y + 16) // colision izquierda inferior
			var resultado2 = jsGFwk._gameObjects.mapa._getGridRelation(this.x + 13, this.y + 16) // colision derecha inferior
			var resultado3 = jsGFwk._gameObjects.mapa._getGridRelation(this.x + 13, this.y) //colision derecha superior
			var resultado4 = jsGFwk._gameObjects.mapa._getGridRelation(this.x + 13, this.y + 2) //colision derecha 
			var resultado5 = jsGFwk._gameObjects.mapa._getGridRelation(this.x + 13, this.y + 14) //colision derecha 
			
			var resultado6 = jsGFwk._gameObjects.mapa._getGridRelation(this.x, this.y + 8) //colision derecha superior
			
			
			var frenado = true;
			
			//console.log(resultado2.x + " " + resultado2.y + " " + resultado2.tile);
			//console.log(jsGFwk.IO.keyboard._activeKey[38]);
			
			console.log (vy + " " + jsGFwk.IO.keyboard._activeKey[38]);
			
			if (tocaTierra == false){
				jsGFwk.IO.keyboard._activeKey[38] = false;
			}
			
			
			// Aca chequea la colision con la matriz y le adiciona gravedad a la caida
			if (jsGFwk._gameObjects.mario.y +16 && resultado1.tile != 1 && resultado2.tile != 1){
				//jumping = true;
				vy += gravity; 
				jsGFwk._gameObjects.mario.y += vy /20;
				frenado = false;
				} else {
					tocaTierra = true;
				}
			// Aca vemos si esta tocando el piso y lo preparamos para saltar
			if (jsGFwk.IO.keyboard._activeKey[38] && jumping == false && jsGFwk._gameObjects.mario.y >= 16){
				frenado = false;
				if (tocaTierra = true){
				vy = 1;
				velY = -5;
				jumping = true;
				jsGFwk.IO.keyboard._activeKey[38] = true;
				}
				//jump();
				
			}
						
			if (jumping == true){
				if(jsGFwk._gameObjects.mario.y + velY > jsGFwk._gameObjects.mario.y - 20 && resultado.tile != 1 && resultado3.tile !=1){
					jsGFwk.IO.keyboard._activeKey[38] = false;
					jsGFwk._gameObjects.mario.y = jsGFwk._gameObjects.mario.y + velY;
					velY = velY - gravity;
					tocaTierra = false;
					
				}else{
					
					jumping = false;
				}
				frenado = false;
				
			}
			// Aca lo movemos hacia la derecha	
			if (jsGFwk.IO.keyboard._activeKey[39] && jsGFwk._gameObjects.mario.x <= 307 && resultado4.tile != 1 && resultado5.tile != 1) {
				frenado = false;
				jsGFwk._gameObjects.mario.x += 3;
			} /*else{
				this.x = Math.floor(this.x /16)*16;
			}*/
			
			// Aca lo movemos hacia la izquierda
			if (jsGFwk.IO.keyboard._activeKey[37] && jsGFwk._gameObjects.mario.x >= 5 && resultado5.tile != 1) {
				frenado = false;
				jsGFwk._gameObjects.mario.x -= 3;
			}/*else{
				this.x = Math.floor(this.x /-16)*16;
			}*/
			
			if (frenado == true){
				jsGFwk._gameObjects.mario.y = Math.floor(jsGFwk._gameObjects.mario.y /16)*16;
				jsGFwk._gameObjects.mario.x = Math.floor(jsGFwk._gameObjects.mario.x /16)*16;
				
			}
			
				
			},
	draw: function (context) {
		
		context.drawImage(jsGFwk.ResourceManager.graphics.sprites.image, 
			nextSpriteX, nextSpriteY, 13, 16,
			jsGFwk._gameObjects.mario.x, jsGFwk._gameObjects.mario.y, 13, 16);
			
	}
};